package light.mvc.controller.patient;


import javax.servlet.http.HttpServletRequest;

import light.mvc.controller.base.BaseController;
import light.mvc.page.base.Grid;
import light.mvc.page.base.Json;
import light.mvc.page.base.PageFilter;
import light.mvc.page.base.SessionInfo;
import light.mvc.page.base.Status;
import light.mvc.page.patient.Register;
import light.mvc.service.patient.MyRegisterServiceI;
import light.mvc.utils.GlobalConstant;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/myRegister")
public class MyRegisterController extends BaseController {

	@Autowired
	private MyRegisterServiceI myRegisterService;
	
	@RequestMapping("/manager")
	public String manager(HttpServletRequest request) {
		SessionInfo sessionInfo = (SessionInfo)request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
		Integer patientId = sessionInfo.getId();
		request.setAttribute("patientId", patientId);
		return "/register/myRegister";
	}
	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public Grid dataGrid(HttpServletRequest request,Register register, PageFilter ph) {
		SessionInfo sessionInfo = (SessionInfo)request.getSession().getAttribute(GlobalConstant.SESSION_INFO);
		Integer patientId = sessionInfo.getId();
		
		Grid grid = new Grid();
		grid.setRows(myRegisterService.dataGrid(patientId,register, ph));
		grid.setTotal((long)myRegisterService.count(patientId, ph));
		
		
		return grid;
	}
	
	@RequestMapping("/detailPage")
	public String detail(HttpServletRequest request, Integer registerId) {
		Register register = myRegisterService.detail(registerId);
		request.setAttribute("treatment", register.getTreatment());
		return "/register/myRegisterDetail";
	}
	
	@RequestMapping("/getSeq")
	@ResponseBody
	public Json getSeq(HttpServletRequest request,Integer registerId) {
		Json j = null;
		String errorMsg = null;
		int errorCode = -1;
		
		Integer seq = null;
		try {
			seq = myRegisterService.edit(registerId);
		} catch (Exception e) {
			errorMsg = e.getMessage();
			j = new Json(new Status(errorCode, errorMsg),null);
			return j;
		}
		
		errorMsg = "取号成功,排序号为"+seq;
		errorCode = 0;
		j = new Json(new Status(errorCode, errorMsg),seq);
		return j;
	}

}
